<template>
	<view class="content">
		<view class="status-bar"></view>
		<view class="tabbar-wrap">
			<view class="tabbar-menu">
				<view class="menu-list">
					<view v-for="(tab, index) in tabBars" :key="tab.id" :class="['menu-item', tabIndex==index ? 'active' : '']" @click="tapTab(index,tab.id)">{{tab.name}}</view>
				</view>
				<view class="post-wrap" @click="openPost">
					<view>
						<image class="post-icon" src="/static/img/post_icon.png" mode=""></image>
						<text>发动态</text>
					</view>
				</view>
			</view>
			<view class="tabbar-content" v-if="tabIndex == 0 ">
				<view class="banner-wrap" v-if="ads.length>0">
					<swiper class="banner-swiper" :indicator-dots="true" indicator-active-color="#ffffff" :autoplay="true" :interval="3000" :duration="600">
						<swiper-item v-for="(item, index) in ads" :key="index" @click="openWebView(item.link)">
							<view class="swiper-item">
								<image class="banner-img" :src="item.file + '?imageView2/0/w/480'" mode="aspectFill"></image>
							</view>
						</swiper-item>
					</swiper>
				</view>
				<view class="topic-wrap">
					<scroll-view class="topic-list" scroll-x="true" :scroll-left="scrollLeft">
						<view :class="['topic-item', 'color4', tid == 0 ? 'active' : '']" @click="topicTab(0)">全部</view>
						<view v-for="(item, index) in topic" :key="index" :class="['topic-item', 'color' + index%4, tid == item.id ? 'active' : '']" @click="topicTab(item.id)">#{{item.name}}#</view>
					</scroll-view>
				</view>
				<view class="dynamics-item" v-for="(item, i) in list" :key="i">
					<view class="user-wrap">
						<image class="avatar" :src="getAvatarUrl(item.user.avatar)" mode="aspectFill" @click="openUserDetails(item.user)"></image>
						<view class="user-info" @click="openUserDetails(item.user)">
							<view class="name-wrap">
								<view class="name">{{item.user.name}}</view>
								<view :class="['sex-wrap', 'sex' + item.user.sex]">
									<image class="sex-icon" :src="'/static/img/sex' + item.user.sex + '.png'" mode=""></image>
									<text class="age">{{item.user.age}}</text>
								</view>
								<view class="sex-wrap" style="margin-left: 10rpx;background-color: #4cbdfc;padding: 2rpx 10rpx;">
									<text class="age">{{location.adname?location.adname:location.city}}</text>
								</view>
								
							</view>
							<view class="time">{{item.created_at | timeFormat}}</view>
						</view>
						<view class="handle-wrap">
							<image v-if="item.is_follow == 0" class="follow-icon" @click="follow(item.user, i)" :src="'/static/img/other'+ item.is_follow +'.png'" mode=""></image>
							<image v-else-if="item.user.id != myInfo.id" class="im-icon" @click="openIM(item.user)" :src="'/static/img/other'+ item.is_follow +'.png'" mode=""></image>
						</view>
					</view>
					<view class="dynamics-content">
						<view class="content">
							<template v-if="!!item.topic">
								<text class="topic" @click="openDynamicsList(item)">#{{item.topic.name}}#</text>
							</template>
							<template v-if="!!item.description">
								<text class="desc" @click="openDynamicsDetail(item)">{{item.description}}</text>
							</template>
							
						</view>
						<view class="media-wrap">
							<view class="media-wrap-bg" @click="openDynamicsDetail(item)"></view>
							<view v-if="item.type == 1" class="img" v-for="(media, j) in item.files" :key="media.id" @click="previewImage(item.files, j)">
								<image :src="media.file + '?imageView2/1/w/300/h/300'" mode=""></image>
							</view>
							<view v-if="item.type == 2" class="video" v-for="(media, k) in item.files" :key="media.id" @click="openDynamicsVideo(media)">
								<image class="cover-image" :src="media.file + '?vframe/jpg/offset/1'" mode="aspectFill"></image>
								<image class="player-icon" src="/static/img/player_icon.png" mode=""></image>
							</view>
						</view>
					</view>
					<view class="dynamics-menu">
						<view class="menu-item" @click="openDynamicsDetail(item)">
							<image class="menu-icon" src="/static/img/comment.png" mode=""></image>
							<text>{{item.comment_size}}</text>
						</view>
						<view class="menu-item" @click="like(item, i)">
							<image class="menu-icon" :src="'/static/img/like'+ item.is_like +'.png'" mode=""></image>
							<text>{{item.like_size}}</text>
						</view>
						<!-- 举报 -->
						<view class="menu-item" @click="report(item.uid)">
							<image class="menu-icon" :src="'/static/img/complaints.png'" mode=""></image>
							<text>举报</text>
						</view>
						<view class="menu-item"></view>
						<view class="menu-item"></view>
					</view>
				</view>
			</view>
			<uni-load-more :status="status" :content-text="contentText"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import util from "@/common/util.js";
	import amap from '@/common/amap/amap.js';
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				amapPlugin: null,
				myInfo: {},
				scrollLeft: 0,
				tabIndex: 0,
				tabBars: [{
					name: '推荐',
					id: 'recommend'
				}],
				tid: 0,
				topic: [],
				ads: [],
				list: [],
				meetList: [], //邂逅的信息
				listPage: 1, //信息页数
				page: 1,
				status: 'more',
				statusInfo: 'more',
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '木有了~'
				},
				empty: false,
				location: {
					lng: '',
					lat: '',
					pname: '',
					city: '',
					adname: '',
					pcode: '',
					citycode: '',
					adcode:'',
				},
			}
		},
		
		onLoad() {
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				
			}
			uni.$on('dynamics-page', data => {
				if (data.type == 'update') {
					this.getDynamic();
				}
			})
		},
		onReady() {
			this.getDynamic();
			this.getRegeo();
		},
		onShow(){
			this.getRegeo();
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				this.getDynamic();
			}, 500)
		},
		onReachBottom() {
			this.status = 'loading';
			this.getDynamic();
		},
		methods: {
			openUserMeet: function (item) {
				if (item.opt == 0) {
					let user = {
						id: item.id,
						name: item.name,
						avatar:item.avatar
					}
					uni.navigateTo({
						url: '../home/user_home?user=' + JSON.stringify(user)
					})
				} else {
					uni.showToast({
						title: '正在相亲',
						position: 'bottom',
						icon: 'none'
					});
				}
			},
			tapTab: function (index,id) {
				if (this.tabIndex == index) {
					return false;
				}
				this.tabIndex = index;
			},
			
			topicTab: function (id) {
				if (this.tid == id) {
					return false;
				}
				this.tid = id;
				this.page = 1;
				this.getDynamic();
			},
			
			getDynamic: function () {
				// uni.showLoading({
				// 	title: ''
				// });
				this.$api.api.ajaxData({
					method: 'V1.Dynamic',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					tid: this.tid,
					page: this.page
				}, res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status) {
						if (this.page == 1) {
							this.topic = res.data.topic
							this.ads = res.data.ads
							this.list = res.data.dynamic
							this.page++
							this.status = 'more'
						} else {
							if (res.data.dynamic.length > 0) {
								for (let i = 0; i < res.data.dynamic.length; i++) {
									this.list.push(res.data.dynamic[i])
								}
								
								this.page++
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
						
						
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			openUserDetails: function (item) {
				let user = {
					id: item.id,
					name: item.name,
					avatar:item.avatar
				}
				uni.navigateTo({
					url: '../home/user_home?user=' + JSON.stringify(user)
				})
			},
			
			follow: function (item, index) {
				// uni.showLoading({
				// 	title: ''
				// });
				this.$api.api.ajaxData({
					method: 'V1.Follow',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					type: 'follow',
					touid: item.id,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						let temp = this.list[index];
						temp.is_follow = 1
						this.$set(this.list, index, temp)
						if (item.id != this.myInfo.id) {
							let custom_ext = {
								fromId: this.myInfo.id,
								fromName: this.myInfo.name,
								fromAvatar: this.myInfo.avatar,
								toId: item.id,
								toName: item.name,
								toAvatar: item.avatar
							}
							let message = this.$tim.createCustomMessage({
								to: this.IDBEFORE + item.id + '',
								conversationType: this.TIM.TYPES.CONV_C2C,
								payload: {
									data: 'like',
									description: '点赞',
									extension: JSON.stringify(custom_ext)
								}
							});
							this.$tim.sendMessage(message).then(imResponse => {
								console.log('点赞消息发送成功', imResponse);
							}).catch(imError => {
								console.warn('点赞消息发送失败', imError);
							});
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			openIM: function (item) {
				let to = {
					conversationID: 'C2C' + this.IDBEFORE + item.id,
					id: this.IDBEFORE + item.id,
					name: item.name,
					avatar: item.avatar
				}
				uni.navigateTo({
					url: '../message/im?to=' + encodeURIComponent(JSON.stringify(to))
				});
			},
			
			openDynamicsDetail: function (item) {
				uni.navigateTo({
					url: 'dynamics_detail?id=' + item.id
				})
			},
			
			openDynamicsList: function (item) {
				uni.navigateTo({
					url: 'dynamics_list?type=topic&tid=' + item.topic.id + '&title=' + item.topic.name
				})
			},
			
			like: function (item, index) {
				let type = ''
				if (item.is_like == 0) {
					type = 'like'
				}
				if (item.is_like == 1) {
					type = 'other'
				}
				// uni.showLoading({
				// 	title: ''
				// });
				this.$api.api.ajaxData({
					method: 'V1.DynamicLike',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					did: item.id,
					type: type,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						if (type == 'like') {
							uni.showToast({
								title: '点赞成功',
								position: 'bottom',
								icon: 'none'
							});
							let temp = this.list[index]
							temp.is_like = 1
							temp.like_size++
							this.$set(this.list, index, temp)
						}
						if (type == 'other') {
							uni.showToast({
								title: '取消点赞',
								position: 'bottom',
								icon: 'none'
							});
							let temp = this.list[index]
							temp.is_like = 0
							temp.like_size--
							this.$set(this.list, index, temp)
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			previewImage: function (list, index) {
				let templist = [];
				for (let i = 0; i < list.length; i++) {
					templist.push(list[i].file);
				}
				uni.previewImage({
					current: index,
					urls: templist,
				});
			},
			
			openDynamicsVideo: function (item) {
				let media = {
					file: item.file
				}
				uni.navigateTo({
					url: '../video/video?media=' + encodeURIComponent(JSON.stringify(media))
				})
			},
			
			openPost: function () {
				uni.navigateTo({
					url: '../dynamics/post?topic=' + JSON.stringify(this.topic)
				})
			},
			
			openWebView: function (url) {
				uni.navigateTo({
					url: '../webview/webview?url=' + encodeURIComponent(url)
				});
			},
			
			getAvatarUrl: function (value) {
				return value.indexOf(this.QINIUURL) == -1 ? value : value + '?imageView2/1/w/100/h/100'
			},
			//////////////////
			getRegeo: function () {
				this.amapPlugin = new amap.AMapWX({
					key: this.AMAPKEY
				});
				if (this.location.lng == '') {
					this.amapPlugin.getRegeo({
						success: (data) => {
							this.location.lng = data[0].longitude;
							this.location.lat = data[0].latitude;
							this.location.pname = data[0].regeocodeData.addressComponent.province;
							this.location.city = data[0].regeocodeData.addressComponent.city;
							this.location.adname = data[0].regeocodeData.addressComponent.district;
							this.location.pcode = data[0].regeocodeData.addressComponent.adcode.substr(0,2) + '0000';
							this.location.citycode = data[0].regeocodeData.addressComponent.adcode.substr(0,4) + '00';
							this.location.adcode = data[0].regeocodeData.addressComponent.adcode;
							
							if (!this.location.city || this.location.city == '0' || this.location.city.length == 0) {
								this.location.city = this.location.pname
							}
							
							this.page = 1;
							// this.getHome('getRegeo');
							console.log('获取定位成功',data[0].regeocodeData.addressComponent);
						},
						fail: (err) => {
							
							setTimeout(() => {
								this.getRegeo();
							}, 10000)
						}
					});
				}
			},
			report(uid){    //举报
				uni.navigateTo({
					url: '../my/complaints?touid=' + uid
				})
			}
		},
		filters: {
			timeFormat: function (value) {
				let now = new Date().getTime();
				let old = value
				let	oldTime = 0
				if (typeof value === 'number') {
					oldTime = old*1000;
				}
				if (typeof value === 'string') {
					oldTime = new Date(value.replace(/-/g, '/')).getTime();
				}
				return util.dateUtils.humanize(now - oldTime);
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.status-bar {
		background-color: #fff;
	}
	.tabbar-menu {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
		padding-top: var(--status-bar-height);
		background-color: #fff;
		border-bottom: 1rpx solid #f7f7f7;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9;
		.menu-list {
			flex: 1;
			display: flex;
			padding: 0 20rpx;
			.menu-item {
				width: 140rpx;
				height: 80rpx;
				line-height: 80rpx;
				font-size: 36rpx;
				text-align: center;
				color: #999;
				position: relative;
				&.active {
					color: #EE3476;
				}
				&.active::after {
					content: '';
					width: 20rpx;
					height: 10rpx;
					background: #EE3476;
					border-radius: 10rpx;
					position: absolute;
				  left: 50%;
				  bottom: 0;
				  margin-left: -10rpx;
				}
			}
		}
		.post-wrap {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 220rpx;
			height: 80rpx;
			background-color: #fff;
			>view{
				color: #fff;
				font-size: 24rpx;
				background: linear-gradient(to right, #5961F9 0%, #C346C2 100%);
				padding: 5rpx 30rpx;
				border-radius: 50rpx;
			}
			.post-icon {
				width: 35rpx;
				height: 35rpx;
				vertical-align: middle;
				margin-right: 10rpx;
				margin-top: -2rpx;
			}
		}
	}
	.tabbar-content {
		padding-top: 80rpx;
	}
	.dynamics-item {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 10rpx;
		.user-wrap {
			display: flex;
			justify-content: center;
			align-items: center;
			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 120rpx;
				background-color: $image-bg-color;
			}
			.user-info {
				flex: 1;
				margin: 0 10rpx;
				.name-wrap {
					// display: flex;
					// justify-content: flex-start;
					// align-items: center;
				
				}
				.name {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
					margin-right: 10rpx;
				}
				.sex-wrap {
					display: flex;
					justify-content: center;
					align-items: center;
					line-height: 20rpx;
					padding: 0rpx 10rpx;
					border-radius: 20rpx;
					display: inline-block;
					margin: -4rpx 0rpx;
					&.sex1 {
						background: #83d7ff;
					}
					&.sex2 {
						background: #fe9ab7;
					}
					.sex-icon {
						width: 18rpx;
						height: 18rpx;
						vertical-align: middle;
					}
					.age {
						font-size: 18rpx;
						color: #fff;
						margin-left: 5rpx;
						
					}
				}
				.time {
					font-size:24rpx;
					color: #666666;
				}
			}
			.handle-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				.follow-icon, .im-icon {
					width: 46rpx;
					height: 38rpx;
				}
			}
		}
		.dynamics-content {
			margin-left: 110rpx;
			.content {
				.desc {
					font-size: 32rpx;
					color: #333;
					line-height: 40rpx;
					margin-right: 10rpx;
				}
				.topic {
					font-size: 32rpx;
					color: #007AFF;
					line-height: 40rpx;
				}
			}
			.media-wrap {
				display: flex;
				flex-wrap: wrap;
				position: relative;
				.media-wrap-bg {
					position: absolute;
					left: 0;
					right: 0;
					top: 0;
					bottom: 0;
					z-index: 1;
				}
				.img {
					width: 200rpx;
					height: 200rpx;
					padding: 5rpx;
					box-sizing: border-box;
					position: relative;
					z-index: 2;
					image {
						display: block;
						width: 190rpx;
						height: 190rpx;
						border-radius: 10rpx;
					}
				}
				.video {
					width: 600rpx;
					height: 360rpx;
					padding: 5rpx;
					box-sizing: border-box;
					position: relative;
					z-index: 2;
					.cover-image {
						display: block;
						width: 590rpx;
						height: 350rpx;
						border-radius: 10rpx;
					}
					.player-icon {
						width: 50rpx;
						height: 50rpx;
						position: absolute;
						left: 50%;
						top: 50%;
						margin-left: -25rpx;
						margin-right: -25rpx;
					}
				}
			}
		}
		.dynamics-menu {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 110rpx;
			.menu-item {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				margin-right: 50rpx;
				.menu-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
				text {
					font-size: 36rpx;
					color: #666;
				}
			}
		}
	}
	
	.topic-wrap {
		padding: 20rpx;
		.title {
			font-size: 30rpx;
			color: #333;
			padding: 0 20rpx;
			margin-bottom: 10rpx;
		}
		.topic-list {
			white-space: nowrap;
			width: 100%;
		}
		.topic-item {
			display: inline-block;
			font-size: 28rpx;
			padding: 5rpx 30rpx;
			background-color: #fff;
			border-radius: 50rpx;
			& + .topic-item {
				margin-left: 20rpx;
			}
			&.color0 {
				color: #0b66fe;
			}
			&.color1 {
				color: #7813fe;
			}
			&.color2 {
				color: #ff3482;
			}
			&.color3 {
				color: #7813fe;
			}
			&.color4 {
				color: #7813fe;
			}
			&.active {
				background-color: #ED4574;
				color: #fff;
			}
		}
	}
	.banner-wrap {
		padding: 10rpx 20rpx;
		background-color: #fff;
		.banner-swiper {
			height: 200rpx;
			.banner-img {
				width: 100%;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>
